﻿<?php
	include_once '../action/checkLogin.php';
	include_once '../../public/inc/DB.php';
	$db = new DB('jusmor_news_cate');
?>
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>新闻分类</title>
    <link type="text/css" rel="stylesheet" href="/public/css/bootstrap.min.css" />
    <script type="text/javascript" src="/public/js/jquery-1.8.3.min.js"></script>
    <script type="text/javascript" src="/public/js/bootstrap.min.js"></script>
    <script type="text/javascript" src="/public/js/tools.js"></script>
</head>
<body>
    <div class="container col-lg-12 ">
        <h3 style="border-bottom:3px solid #336699; padding-bottom:2px;"><strong>添加分类</strong>&nbsp;<small><button class="btn btn-primary btn-sm" data-toggle="modal" data-target="#addModal"> 添加分类</button></small></h3>
        <div class="table-responsive">
            <table class="table table-bordered table-hover">
                <thead style="background:#EEEEEE;">
                    <th>ID</th>
                    <th>分类名称</th>
                    <th>操作</th>
                </thead>
                <tbody>
                <?php 
                	$fields = 'news_cate_id, news_cate_name';
                	$order = array('create_time'=>'asc');
                	$result = $db->fetchAll('', $fields, '', '', '', $order);
					if(! empty($result)) {
                		foreach ($result as $key=>$val) {
                ?>
                    <tr>
                        <td class="lb_id"><?php echo $val['news_cate_id'];?></td>
                        <td class="lb_text"><?php echo $val['news_cate_name'];?></td>
                        <td>
                            <button type="button" class="btn btn-primary btn-xs updateModel">修改</button>
                            <button type="button" class="btn btn-danger btn-xs delCate">删除</button>
                        </td>
                    </tr>
                <?php 
						}
					} else {
                ?>
                	<tr class="text-center"><td colspan="3">没有分类！</td></tr>
                <?php
					}
				?>
                </tbody>
            </table>
        </div>
    </div>

    <!--添加分类-->
    <div class="modal fade" id="addModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close closeModal" data-dismiss="modal" aria-hidden="true">&times;</button>
                    <h4 class="modal-title" id="myModalLabel"><strong>添加分类</strong></h4>
                </div>
                <div class="modal-body">
                    <form class="form-horizontal" role="form">
                        <div class="form-group">
                            <label class="col-sm-2 control-label">分类名称:</label>
                            <div class="col-xs-8">
                                <input type="text" class="form-control" name="cate_name" placeholder="输入分类名称……">
                            </div>
                        </div>
                    </form>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default closeModal" data-dismiss="modal">关闭</button>
                    <button type="button" class="btn btn-primary saveAddCate">保存</button>
                </div>
            </div>
        </div>
    </div>

    <!--修改分类-->
    <div class="modal fade" id="updateModel" tabindex="-1" role="dialog" aria-hidden="true" >
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close closeModal" data-dismiss="modal" aria-hidden="true">&times;</button>
                    <h4 class="modal-title" ><strong>修改分类</strong></h4>
                </div>
                <div class="modal-body">
                    <form class="form-horizontal" role="form">
                        <div class="form-group">
                            <label class="col-sm-2 control-label">分类名称:</label>
                            <div class="col-xs-8">
                                <input type="text" name="cate_name" class="form-control" placeholder="分类名称" data-id ="" value="">
                            </div>
                        </div>
                    </form>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default closeModal" data-dismiss="modal">关闭</button>
                    <button type="button" class="btn btn-primary saveUpdateCate">保存</button>
                </div>
            </div>
        </div>
    </div>
 
    <script type="text/javascript">
        $(function () {
            //添加分类
            $(".saveAddCate").click(function () {
                var _this = $(this);
                var _input_cate_name = _this.closest(".modal").find("input[name='cate_name']");
                if (!empty(_input_cate_name.val())) {
                    var _url = "../ajaxAPI/newsAPI.php";
                    var _param = { "cate_name": _input_cate_name.val(), "action": "addcate" };
                    $.post(_url, _param, function (data) {
                        if (data.code == 1) {
                            alert("添加成功！");
                            refresh();
                        } else {
                            alert(data.msg);
                        }
                    }, "json")
                } else {
                    _input_cate_name.parent().addClass("has-error");
                    _input_cate_name.attr("placeholder", "请输入分类名称！").focus();
                }
            })


            //修改分类
            $(".updateModel").click(function () {
                var _this = $(this);
                var _tr = _this.closest("tr");
                var _id = _tr.find(".lb_id").text();
                var _text = _tr.find(".lb_text").text();
                $("#updateModel").find("input[name='cate_name']").attr("data-id", _id).val(_text);

                $("#updateModel").modal();
            })

            //保存修改分类
            $(".saveUpdateCate").click(function () {
                var _this = $(this);
                var _input_cate_name = _this.closest(".modal").find("input[name='cate_name']");
                var _cate_id = _input_cate_name.attr("data-id");
                if (!empty(_input_cate_name.val()) && !empty(_cate_id)) {
                    var _url = "../ajaxAPI/newsAPI.php";
                    var _param = { "cate_name": _input_cate_name.val(), "cate_id": _cate_id, "action": "updatecate" };
                    $.post(_url, _param, function (data) {
                        if (data.code == 1) {
                            alert("修改成功！");
                            refresh();
                        } else {
                            alert(data.msg);
                        }
                    }, "json")
                } else {
                    _input_cate_name.parent().addClass("has-error");
                    _input_cate_name.attr("placeholder", "请输入分类名称！").focus();
                }
            })

            //删除分类
            $(".delCate").click(function () {
                var _this = $(this);
                if (confirm("确认删除此分类！")) {
                    var _tr = _this.closest("tr");
                    var _cate_id = _tr.find(".lb_id").text();
                    if (!empty(_cate_id)) {
                        var _url = "../ajaxAPI/newsAPI.php";
                        var _param = { "cate_id": parseInt(_cate_id), "action": "delcate" };
                        $.post(_url, _param, function (data) {
                            if (data.code == 1) {
                                _tr.hide("500", function () {
                                    _tr.remove();
									alert("删除成功！");
                                })
                            } else {
                                alert(data.msg);
                            }
                        }, "json")
                    } else {
                        alert("未知错误，请刷新重试！");
                    }
                    
                }
            })

            //关闭模态窗口，清空所有input标签的值
            $(".closeModal").click(function () {
                $(this).closest(".modal").find(":text").val("");
            })
        })
    </script>
</body>
</html>
